package top.cardone.generator.code.util;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import top.cardone.generator.code.mapper.EntityMapper;
import top.cardone.generator.code.mapper.FieldMapper;

import java.util.List;
import java.util.Map;

/**
 * Created by yao hai tao on 2015/8/21.
 */
public class EntityMapperUtils {
    public static String getPkType(EntityMapper entityMapper) {
        if (entityMapper == null || CollectionUtils.isEmpty(entityMapper.getFieldMapperMap())) {
            return null;
        }

        for (Map.Entry<String, FieldMapper> fieldMapperEntry : entityMapper.getFieldMapperMap().entrySet()) {
            if (StringUtils.isNotBlank(fieldMapperEntry.getValue().getPkName())) {
                return fieldMapperEntry.getValue().getTypeCode();
            }
        }

        return null;
    }

    public static String getPks(EntityMapper entityMapper) {
        if (entityMapper == null || CollectionUtils.isEmpty(entityMapper.getFieldMapperMap())) {
            return null;
        }

        List<String> pkList = Lists.newArrayList();

        for (Map.Entry<String, FieldMapper> fieldMapperEntry : entityMapper.getFieldMapperMap().entrySet()) {
            if (StringUtils.isNotBlank(fieldMapperEntry.getValue().getPkName())) {
                pkList.add(fieldMapperEntry.getValue().getCode());
            }
        }

        return StringUtils.join(pkList, ",");
    }
}
